Inventory = {}

usermessage.Hook( "AddItem", function(data)
	for k,v in pairs(Basewars.Items) do
		if v.ID == data:ReadChar() then
			if v.Stackable && HasItem(v.Class) then
				for inv,ite in pairs(Inventory) do
					if ite.Class == v.Class then
						ite.Stack = ite.Stack + 1
					end
				end
			else
				table.insert(Inventory,v)
			end
		end
	end
end)


usermessage.Hook( "RemoveItem", function(data)
	for k,v in pairs(Basewars.Items) do
		if v.ID == data:ReadChar() then
			for inv,ite in pairs(Inventory) do
				if ite.Class == v.Class then
					if ite.Stackable && ite.Stack > 1 then
						Msg("Removing Stack")
						ite.Stack = ite.Stack - 1
					else
						Msg("Removing Item")
						table.remove(Inventory,inv)
					end
				end
			end
		end
	end
end)

function HasItem(item)
	for k,v in pairs(Inventory) do
		if v.Class == item then
			return true
		end
	end
	return false		
end

function FindItem(item)
	for k,v in pairs(Basewars.Items) do
		if v.Class == item then
			return v
		else
			return false
		end
	end
end

usermessage.Hook( "ShowInv", function (data)
local slot = {}
	local i = 1
		DermaPanel = vgui.Create("DFrame")
		DermaPanel:SetPos( 50,50 )
		DermaPanel:SetSize( 345,310)
		DermaPanel:SetTitle("Inventory")
		DermaPanel:SetVisible( true )
		DermaPanel:ShowCloseButton( true)
		DermaPanel:Center()
		DermaPanel:MakePopup()
		
		grid = vgui.Create( "DGrid", DermaPanel )
		grid:SetPos( 10, 30 )
		grid:SetCols( 6 )
		grid:SetColWide( 55 )
		grid:SetRowHeight( 55 )
 
	while(i <= 30) do
		slot[i] = vgui.Create( "item" )
		slot[i]:SetSize( 50, 50 )
		grid:AddItem( slot[i] )
		i = i + 1
	end
	for k,v in pairs(Inventory) do
		slot[k]:SetupModel(v.Model,v,v.Fov,v.CamPos,v.LookAt)
	end
end)